//
//  MBView.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/3/27.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "MBView.h"

@interface MBView ()<UIScrollViewDelegate>
{
    UIImageView *iView1, *iView2, *iView3, *iView4;
}

@property (nonatomic, strong) UIScrollView  *scrollView;

@end

@implementation MBView

- (UIScrollView *)scrollView
{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        _scrollView.delegate = self;
        _scrollView.backgroundColor = BACKGROUND_COLOR;
        _scrollView.scrollEnabled = YES;
    }
    return _scrollView;
}

- (void)addLabel
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
    label.text = @"制动模式";
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    label.textColor = TEXT_GRAY_COLOR;
    [self.scrollView addSubview:label];
    
    label = nil;
}

- (void)addView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.bounds.size.width, self.bounds.size.height / 4)];
    view.backgroundColor = [UIColor whiteColor];
    
    /************************************************************/
    
    /************************************************************/
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    label.center = CGPointMake(view.bounds.size.width / 2, 20);
    label.text = @"幕布";
    label.textColor = [UIColor blackColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    [view addSubview:label];
    label = nil;
    
    for (int i = 0; i < 2; i++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i * (view.bounds.size.width) / 2,
                                                                      view.bounds.size.height / 3,
                                                                      (view.bounds.size.width - 1) / 2,
                                                                      view.bounds.size.height / 3 * 2 - 10)];
        button.tag = i;
        UIImage *image1, *image2;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(button.bounds.size.width / 2 - 25,
                                                                   button.bounds.size.height - 25, 50, 20)];
        label.tag = 20 + i;
        label.textAlignment = NSTextAlignmentCenter;
        if(i == 0){
            image1 = GET_IAMGE_WITH_NAME(@"Image/fundationView/curtain/btn_open_curtain_n");
            image2 = GET_IAMGE_WITH_NAME(@"Image/fundationView/curtain/btn_open_curtain_p");
            label.text = @"打开";
        }else{
            image1 = GET_IAMGE_WITH_NAME(@"Image/fundationView/curtain/btn_close_curtain_n");
            image2 = GET_IAMGE_WITH_NAME(@"Image/fundationView/curtain/btn_close_curtain_p");
            label.text = @"关闭";
        }
        UIImageView *imageView1 = [[UIImageView alloc] initWithImage:image1];
        UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image2];
        [imageView1 setFrame:CGRectMake(button.bounds.size.width / 2 - 25, 0, 50, 50)];
        [imageView2 setFrame:CGRectMake(button.bounds.size.width / 2 - 25, 0, 50, 50)];
        label.textColor = TEXT_GRAY_COLOR;
        if(i == 0){
            iView1 = imageView1;
            iView2 = imageView2;
        }else{
            iView3 = imageView1;
            iView4 = imageView2;
        }
        [button addTarget:self action:@selector(touchDownButton:) forControlEvents:UIControlEventTouchDown];
        [button addTarget:self action:@selector(touchUpInsideButton:) forControlEvents:UIControlEventTouchUpInside];
        [button addTarget:self action:@selector(touchUpOutsideButton:) forControlEvents:UIControlEventTouchUpOutside];
        [button addSubview:imageView1];
        [button addSubview:label];
        [view addSubview:button];
    }
    
    [self.scrollView addSubview:view];
    view = nil;
}

#pragma mark - 按钮响应函数
- (void)touchDownButton:(UIButton *)button
{
    UILabel *label = (UILabel *)[button viewWithTag:button.tag + 20];
    NSUInteger count = button.subviews.count;
    for(int i = 0; i < count; i++){
        [button.subviews[0] removeFromSuperview];
    }
    label.textColor = TEXT_GREEN_COLOR;
    [button addSubview:label];
    if(button.tag == 0){
        [button addSubview:iView2];
    }else{
        [button addSubview:iView4];
    }
}

- (void)touchUpInsideButton:(UIButton *)button
{
    UILabel *label = (UILabel *)[button viewWithTag:button.tag + 20];
    NSUInteger count = button.subviews.count;
    for(int i = 0; i < count; i++){
        [button.subviews[0] removeFromSuperview];
    }
    label.textColor = TEXT_GRAY_COLOR;
    [button addSubview:label];
    if(button.tag == 0){
        [button addSubview:iView1];
        //打开
        [self.delegate sendRespondByFuctionName:MB_ON];
    }else{
        [button addSubview:iView3];
        //关闭
        [self.delegate sendRespondByFuctionName:MB_OFF];
    }
}

- (void)touchUpOutsideButton:(UIButton *)button
{
    UILabel *label = (UILabel *)[button viewWithTag:button.tag + 20];
    NSUInteger count = button.subviews.count;
    for(int i = 0; i < count; i++){
        [button.subviews[0] removeFromSuperview];
    }
    label.textColor = TEXT_GRAY_COLOR;
    [button addSubview:label];
    if(button.tag == 0){
        [button addSubview:iView1];
    }else{
        [button addSubview:iView3];
    }
}

- (instancetype) initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self addSubview:self.scrollView];
        [self addLabel];
        [self addView];
    }
    
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
//- (void)drawRect:(CGRect)rect {
//    // Drawing code
//}


@end
